package 课程设计报告_hb_6_30;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jiang Zhiyan
 *
 * 2021年6月30日 下午3:20:18
 *
 */
public class TestMap {

	public List<Apple> initData() {
		List<Apple> apps = new ArrayList<Apple>();
		apps.add(new Apple("绿色", 150, "山西"));
		apps.add(new Apple("绿色", 100, "山西"));
		apps.add(new Apple("绿色", 90, "山西"));
		apps.add(new Apple("红色", 150, "山西"));
		apps.add(new Apple("红色", 100, "山西"));
		apps.add(new Apple("红色", 90, "山西"));
		apps.add(new Apple("绿色", 150, "陕西"));
		apps.add(new Apple("绿色", 100, "陕西"));
		apps.add(new Apple("绿色", 90, "陕西"));
		apps.add(new Apple("红色", 150, "陕西"));
		apps.add(new Apple("红色", 100, "陕西"));
		apps.add(new Apple("红色", 90, "陕西"));
		apps.add(new Apple("绿色", 150, "陕西"));

		apps.add(new Apple("绿色", 100, "新疆"));
		apps.add(new Apple("绿色", 90, "新疆"));
		apps.add(new Apple("红色", 150, "新疆"));
		apps.add(new Apple("红色", 100, "新疆"));
		apps.add(new Apple("红色", 90, "新疆"));
		return apps;
	}

	/**
	 * 获得每个产地的苹果的信息
	 * @param datas
	 * @return
	 */
	public Map<String, Apple> locationApples(List<Apple> datas) {
        Map<String ,List<Apple>> map = new HashMap<>();
        for(Apple apple : datas) {
//        	1、获得所在的地区
        	String location = apple.getLocation();
//        	2、判断location是否在map中存在
        	if(map.containsKey(location)) {
        		//获得对应的list
        		List<Apple>  list = map.get(location);
//        		如果存在，追加到对应的value中（list中）
        		list.add(apple);
        	}
        	 else {
//        		在map中添加数据，map#put(location,list)
        		List<Apple> list  = new ArrayList<Apple>();
        		list.add(apple);
        		map.put(location, list);
        	}
        }
		return null;
	}
	
}
